/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.expert.service;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.modules.expert.dao.SolutionFaceProductMixDetailDao;
import com.chlitina.store.modules.expert.entity.SolutionFaceProductMixDetail;

/**
 *  方案产品步骤详细Service
 * @author liujiang
 * @version 2013-08-19
 */
@Component
@Transactional(readOnly = true)
public class SolutionFaceProductMixDetailService extends BaseService {

	@SuppressWarnings("unused")
	private static Logger logger = LoggerFactory.getLogger(SolutionFaceProductMixDetailService.class);
	
	@Autowired
	private SolutionFaceProductMixDetailDao solutionFaceProductMixDetailDao;
	
	public SolutionFaceProductMixDetail get(Long id) {
		return solutionFaceProductMixDetailDao.findOne(id);
	}
	
	public List<SolutionFaceProductMixDetail> getByMix(long mixId, long storeTypeId){
		return solutionFaceProductMixDetailDao.getByMix(mixId, storeTypeId);
	}
	
	public SolutionFaceProductMixDetail getBySolutionFaceProductMixDetail(SolutionFaceProductMixDetail sd) {
		DetachedCriteria dc = solutionFaceProductMixDetailDao.createDetachedCriteria();
		if(sd.getProduct() != null) {
			dc.add(Restrictions.eq("product", sd.getProduct()));
		}
		if(sd.getSolutionFaceProductMix()!= null) {
			dc.add(Restrictions.eq("solutionFaceProductMix", sd.getSolutionFaceProductMix()));
		}
		List<SolutionFaceProductMixDetail> list = solutionFaceProductMixDetailDao.find(dc);
		if(list.size() > 0) {
			return list.get(0);
		}
		return null;
	}
	
	public Page<SolutionFaceProductMixDetail> find(Page<SolutionFaceProductMixDetail> page, SolutionFaceProductMixDetail solutionFaceProductMixDetail) {
		DetachedCriteria dc = solutionFaceProductMixDetailDao.createDetachedCriteria();
		dc.add(Restrictions.eq("delFlag", SolutionFaceProductMixDetail.DEL_FLAG_NORMAL));
		dc.addOrder(Order.desc("id"));
		return solutionFaceProductMixDetailDao.find(page, dc);
	}
	
	@Transactional(readOnly = false)
	public void save(SolutionFaceProductMixDetail solutionFaceProductMixDetail) {
		solutionFaceProductMixDetailDao.save(solutionFaceProductMixDetail);
		solutionFaceProductMixDetail.getSolutionFaceProductMix().getDetailList().add(solutionFaceProductMixDetail);
	}
	
	@Transactional(readOnly = false)
	public void delete(SolutionFaceProductMixDetail de) {
		solutionFaceProductMixDetailDao.delete(de);
	}
}
